export default {
    getCurrentDatestring() {
        return new Date().toISOString().split('T')[0];
    },

	stringToTimestamp(dateStr) {
		const date = new Date(dateStr+' 00:00:00');
		return Math.floor(date.getTime() / 1000);
	},
	stringToDatetime(dateStr) {
		return new Date(dateStr+" 00:00:00");
	},
	timestampToString(timestamp) {
		const datetime = new Date(timestamp*1000);
		return this.toISOString(datetime);
	},
	dateToString(date) {
		return this.toISOString(date);
	},

    getYear(dateStr) {
        const date = new Date(dateStr + " 00:00:00");
        return date.getFullYear();
    },

    getMonth(dateStr) {
        const date = new Date(dateStr + " 00:00:00");
        return date.getMonth() + 1; // JavaScript 中月份是从 0 开始的
    },

    getMonthDay(dateStr) {
        const date = new Date(dateStr + " 00:00:00");
        return date.getDate();
    },
	getWeek(dateStr) {
		let date = new Date(dateStr + " 00:00:00");
		let week = date.toLocaleString('en-US', {
			week: 'numeric'
		});
		return parseInt(week, 10);
	},
	getWeekday(dateStr) {
		let date = new Date(dateStr + " 00:00:00");
		let weekday = date.toLocaleString('en-US', {
			weekday: 'long'
		});
		// Map the weekday string to a number (1-7) where 1 is Monday and 7 is Sunday
		const weekdayNumbers = {
			'Monday': 1,
			'Tuesday': 2,
			'Wednesday': 3,
			'Thursday': 4,
			'Friday': 5,
			'Saturday': 6,
			'Sunday': 7
		};
		return weekdayNumbers[weekday];
	},

    parse(dateStr) {
        const patterns = [
            'yyyy-MM-dd HH:mm:ss', // 2024-01-01
            'yyyy/MM/dd HH:mm:ss', // 2024/01/01
        ];
        for (const pattern of patterns) {
            const date = new Date(dateStr + " 00:00:00");
            if (!isNaN(date.getTime())) {
                return date;
            }
        }
        throw new Error(`Invalid date format: ${dateStr}`);
    },

    format(date) {
		// return date.toISOString().split('T')[0];
		return this.toISOString(date);
    },
    addDays(dateStr, amount) {
        const date = new Date(dateStr + " 00:00:00");
        date.setDate(date.getDate() + amount);
        // return date.toISOString().split('T')[0];
		return this.toISOString(date);
    },

    addWeeks(dateStr, amount) {
        const date = new Date(dateStr + " 00:00:00");
        date.setDate(date.getDate() + (amount * 7));
        // return date.toISOString().split('T')[0];
		return this.toISOString(date);
    },

    addMonths(dateStr, amount) {
        const date = new Date(dateStr + " 00:00:00");
        date.setMonth(date.getMonth() + amount);
		date.setDate(date.getDate());
        // return date.toISOString().split('T')[0];
		return this.toISOString(date);
    },

    addYears(dateStr, amount) {
        const date = new Date(dateStr + " 00:00:00");
        date.setFullYear(date.getFullYear() + amount);
		date.setDate(date.getDate());
        // return date.toISOString().split('T')[0];
		return this.toISOString(date);
    },

    subDateYears(beginDateStr, endDateStr) {
        const beginDate = new Date(beginDateStr + " 00:00:00");
        const endDate = new Date(endDateStr + " 00:00:00");
        return Math.abs(endDate.getFullYear() - beginDate.getFullYear());
    },

    subDateMonths(beginDateStr, endDateStr) {
        const beginDate = new Date(beginDateStr + " 00:00:00");
        const endDate = new Date(endDateStr + " 00:00:00");
        const yearsDiff = endDate.getFullYear() - beginDate.getFullYear();
        const monthsDiff = endDate.getMonth() - beginDate.getMonth();
        return Math.abs(yearsDiff * 12 + monthsDiff);
    },

    subDateDays(beginDateStr, endDateStr) {
        const beginDate = new Date(beginDateStr + " 00:00:00");
        const endDate = new Date(endDateStr + " 00:00:00");
        const diffTime = Math.abs(endDate - beginDate);
        const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
        return diffDays;
    },

    getIntervalDates(beginDateStr, endDateStr) {
        const beginDate = new Date(beginDateStr + " 00:00:00");
		beginDate.setDate(beginDate.getDate());
        const endDate = new Date(endDateStr + " 00:00:00");
		endDate.setDate(endDate.getDate());
        const dateArray = [];
        for (let date = new Date(beginDate); date <= endDate; date.setDate(date.getDate() + 1)) {
            // dateArray.push(date.toISOString().split('T')[0]);
			dateArray.push(this.toISOString(date));
        }
        return dateArray;
    },

    getDatesFromSepdate(dateStr, beginDateSep, endDateSep) {
        const centerDate = new Date(dateStr + " 00:00:00");
        const dates = [];
        for (let i = 1; i <= beginDateSep; i++) {
            const pastDate = new Date(centerDate);
            pastDate.setDate(pastDate.getDate() - i);
            // dates.unshift(pastDate.toISOString().split('T')[0]);
			dates.unshift(this.toISOString(pastDate));
        }
        for (let i = 0; i <= endDateSep; i++) {
            const futureDate = new Date(centerDate);
            futureDate.setDate(futureDate.getDate()+i);
            // dates.push(futureDate.toISOString().split('T')[0]);
			dates.push(this.toISOString(futureDate));
        }
        return dates;
    },

    // 由于 JavaScript 的 Date 对象不支持直接转换 PHP 模式到 SimpleDateFormat，
    // 这个函数留空，如果需要可以自定义实现。
    _parsePattern(javaPattern) {
        // 这里可以添加转换逻辑
    },
	toISOString(d) {
		var z = n => ('0' + n).slice(-2);
		var zz = n => ('00' + n).slice(-3);
		var off = d.getTimezoneOffset();
		var sign = off > 0 ? '-' : '+';
		off = Math.abs(off);
	
		return d.getFullYear() + '-' +
			z(d.getMonth() + 1) + '-' +
			z(d.getDate());
	}
};